﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Z_字形变换
{
    class Program
    {
        static void Main(string[] args)
        {

            string test = "PAYPALISHIRING";
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            Solution solution = new Solution();
            Console.WriteLine(solution.Convert(test,5));
            stopwatch.Stop();
            Console.WriteLine(stopwatch.ElapsedMilliseconds + "ms");
            Console.ReadLine();
        }
    }

    public class Solution
    {
        public string Convert(string s, int numRows)
        {
            StringBuilder str = new StringBuilder();
            List<List<char>> list = new List<List<char>>(); 
            int num1 = s.Length / numRows;
            if (num1 > 0)
            {
                int num = numRows + numRows - 2;
                if (num > 0)
                {
                    int num2 = s.Length / num;
                    for (int i = 0; i < numRows; i++)
                    {
                        List<char> l = new List<char>();
                        list.Add(l);
                    }
                    if (num2 > 0)
                    {
                        for (int i = 0; i < num2; i++)
                        {
                            string b = s.Substring(i * num, num);
                            for (int j = 0; j < b.Length; j++)
                            {
                                if (j >= numRows)
                                {
                                    list[numRows - 2 - (j - numRows)].Add(b[j]);
                                }
                                else
                                {
                                    list[j].Add(b[j]);
                                }
                            }
                        }
                    }
                    int num3 = s.Length % num;
                    for (int i = 0; i < num3; i++)
                    {
                        if (i >= numRows)
                        {
                            list[numRows - 2 - (i - numRows)].Add(s[num2 * num + i]);
                        }
                        else
                        {
                            list[i].Add(s[num2 * num + i]);
                        }
                    }
                }
                else
                {
                    return s;
                }
            
                for (int i = 0; i < list.Count; i++)
                {
                    for (int j = 0; j < list[i].Count; j++)
                    {
                        str.Append(list[i][j]);
                        //Console.Write(list[i][j]);
                    }
                    //Console.WriteLine();
                }
                return str.ToString();
            }
            else
            {
                return s;
            }
        }
    }
}
